package org.ayachinene.leetcode.problems;

import org.ayachinene.leetcode.structure.ListNode;

public class P1669 {
    static class Solution {
        public ListNode mergeInBetween(ListNode l1, int a, int b, ListNode l2) {
            ListNode p1 = l1;
            int i = 0;
            while (i < a - 1) {
                p1 = p1.next;
                i++;
            }
            ListNode tail = p1;
            while (i < b + 1) {
                p1 = p1.next;
                i++;
            }
            ListNode left = p1;
            ListNode p2 = l2;
            while (p2 != null) {
                tail.next = p2;
                tail = p2;
                p2 = p2.next;
            }
            tail.next = left;
            return l1;
        }
    }
}
